package com.bjpowernode.controller;

import com.bjpowernode.entity.TActivity;
import com.bjpowernode.entity.TActivityDO;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 分页查询市场活动
     *
     * @param current
     * @return
     */
    @GetMapping(value = "/api/activitys")
    public R activityPage(@RequestParam(value = "current", required = false) Integer current,
                          ActivityQuery activityQuery) {
        // required = false 表示current参数可以传，也可以不传，也就是current参数可以是空的；
        // required = true 表示current参数必须传，不传会报错；
        if (current == null) {
            current = 1;
        }
        PageInfo<TActivity> pageInfo = activityService.getActivityPage(current, activityQuery);
        return R.OK(pageInfo);
    }

    @GetMapping(value = "/api/activity/{id}")
    public R activityDetail(@PathVariable(value = "id") Integer id) {
        TActivity tActivity = activityService.getActivityById(id);
        return R.OK(tActivity);
    }

    @PostMapping(value = "/api/activity")
    public R addActivity(ActivityQuery activityQuery) {
        int save = activityService.saveActivity(activityQuery);
        return save >= 1 ? R.OK() :R.FAIL();
    }

    @PutMapping(value = "/api/activity")
    public R editActivity(ActivityQuery activityQuery) {
        int update = activityService.updateActivity(activityQuery);
        return update >= 1 ? R.OK() :R.FAIL();
    }

    @DeleteMapping(value = "/api/activity/{id}")
    public R delActivity(@PathVariable(value = "id") Integer activityId) {
        int del = activityService.delActivityById(activityId);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/api/activity")
    public R batchDelActivity(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,8,12";
        List<String> idList = Arrays.asList(ids.split(","));
        int del = activityService.batchDelActivityByIds(idList);
        return del >= idList.size() ? R.OK() : R.FAIL();
    }

    @GetMapping(value = "/api/activity/ongoing")
    public R ongoingActivity() {
        List<TActivityDO> tActivityList = activityService.getOngoingActivity();
        return R.OK(tActivityList);
    }
}
